//
//  PKPricingPlansCollectionViewCell.swift
//  GoAbroad
//
//  Created by Pink Snow on 2025/5/14.
//

import UIKit
import RxSwift
import RxCocoa
import NSObject_Rx
import NVActivityIndicatorView
struct PricingPlan {
    let title: String
    let price: String
    let description: String
    let features: [String]
    let isPopular: Bool
    let buttonTitle: String
}
class PKPricingPlansCollectionViewCell: UICollectionViewCell   {
    static let identifier = "PKPricingPlansCollectionViewCell"
        private let titleLabel = UILabel()
        private let priceLabel = UILabel()
        private let descriptionLabel = UILabel()
        private let featuresStack = UIStackView()
    private let actionButton = PopRippleButton.createRipple(type: .custom, title: "尼玛")
        private let badgeLabel = UILabel()
    let cellTapBehavior  = BehaviorRelay<String>(value: "")
    var disposeBag = DisposeBag()
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupUI()
            
            actionButton.rippleColor = .systemRed
            actionButton.rippleDuration  = 0.8
            
//            actionButton.rx.tap.throttle(.seconds(5), scheduler: MainScheduler.instance).bind(to: cellTapBehavior).disposed(by: disposeBag)
            actionButton.rx.tap.subscribe { [weak self] _ in
                guard let self = self else { return }
                      self.actionButton.isEnabled = false
                      // 执行点击逻辑，比如触发 buttonTap.onNext()
                      DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                          self.actionButton.isEnabled = true
                      }
                cellTapBehavior.accept("tap")
            }onError: { error in
                
            }onCompleted: {
            
            }.disposed(by: disposeBag)
            
//            actionButton.rx.tap.subscribe { [self] _ in
//                print("点击")
//                
////                self.loding
//                
//                

//                
//                
//                
//            }.disposed(by: disposeBag)

        }
        
        required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
        
    func configure(with plan: PricingPlan,_ modal : PKPriceModal) {
        titleLabel.text = modal.name
        //String(modal.price) +
        priceLabel.text =  "9.99 $"
            descriptionLabel.text = modal.features
            badgeLabel.isHidden = !plan.isPopular
        actionButton.setTitle(modal.buttonText, for: .normal)
   
//        actionButton.adjustsImageWhenHighlighted = true
//        actionButton.back
            
            featuresStack.arrangedSubviews.forEach { $0.removeFromSuperview() }
            for feature in plan.features {
                let label = UILabel()
                label.text = "✓ \(feature)"
                label.font = .systemFont(ofSize: 14)
                label.textColor = .darkGray
                featuresStack.addArrangedSubview(label)
            }
        }
    override func prepareForReuse() {
        super.prepareForReuse()
        // 忘了清除订阅
        disposeBag = DisposeBag()
    }

        private func setupUI() {
            contentView.backgroundColor = .white
            contentView.layer.cornerRadius = 16
            contentView.layer.shadowColor = UIColor.black.cgColor
            contentView.layer.shadowOpacity = 0.1
            contentView.layer.shadowOffset = .zero
            contentView.layer.shadowRadius = 6

            titleLabel.font = .boldSystemFont(ofSize: 20)
            priceLabel.font = .systemFont(ofSize: 18)
            priceLabel.textColor = .systemOrange
            descriptionLabel.font = .systemFont(ofSize: 14)
            descriptionLabel.textColor = .gray
            descriptionLabel.numberOfLines = 2

            featuresStack.axis = .vertical
            featuresStack.spacing = 6
            
            actionButton.setTitleColor(.white, for: .normal)
            actionButton.backgroundColor = .systemOrange
            actionButton.layer.cornerRadius = 8
//            actionButton.setBackgroundImage(UIColor(hexString: "ee4433").toImage(), for: .normal)
//            actionButton.setBackgroundImage(UIColor.systemOrange.toImage(), for: .selected)
            actionButton.titleLabel?.font = .boldSystemFont(ofSize: 16)
            
            badgeLabel.text = "Most Popular"
            badgeLabel.font = .systemFont(ofSize: 12)
            badgeLabel.textColor = .white
            badgeLabel.backgroundColor = .systemOrange
            badgeLabel.textAlignment = .center
            badgeLabel.layer.cornerRadius = 10
            badgeLabel.clipsToBounds = true
            
            contentView.addSubview(titleLabel)
            contentView.addSubview(priceLabel)
            contentView.addSubview(descriptionLabel)
            contentView.addSubview(featuresStack)
            contentView.addSubview(actionButton)
            contentView.addSubview(badgeLabel)

            badgeLabel.snp.makeConstraints { make in
                make.top.equalToSuperview().offset(10)
                make.centerX.equalToSuperview()
                make.height.equalTo(20)
                make.width.greaterThanOrEqualTo(100)
            }

            titleLabel.snp.makeConstraints { make in
                make.top.equalTo(badgeLabel.snp.bottom).offset(12)
                make.left.right.equalToSuperview().inset(16)
            }
            priceLabel.snp.makeConstraints { make in
                make.top.equalTo(titleLabel.snp.bottom).offset(8)
                make.left.right.equalTo(titleLabel)
            }
            descriptionLabel.snp.makeConstraints { make in
                make.top.equalTo(priceLabel.snp.bottom).offset(8)
                make.left.right.equalTo(titleLabel)
            }
            featuresStack.snp.makeConstraints { make in
                make.top.equalTo(descriptionLabel.snp.bottom).offset(12)
                make.left.right.equalTo(titleLabel)
            }
            actionButton.snp.makeConstraints { make in
                make.top.greaterThanOrEqualTo(featuresStack.snp.bottom).offset(16)
                make.left.right.equalTo(titleLabel)
                make.bottom.equalToSuperview().inset(16)
                make.height.equalTo(44)
            }
        }
    }

